package com.jiazhong.mybatis.m2;

import com.jiazhong.mybatis.m2.bean.Dept;
import com.jiazhong.mybatis.m2.bean.Emp;
import com.jiazhong.mybatis.m2.mapper.DeptMapper;
import com.jiazhong.mybatis.m2.mapper.EmpMapper;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;
import java.util.List;
import java.util.Set;

public class App {

    private static void a() {
        InputStream inputStream = App.class.getClassLoader().getResourceAsStream("config.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        EmpMapper empMapper = sqlSession.getMapper(EmpMapper.class);
        List<Emp> emps = empMapper.findAll2();
        emps.forEach(System.out::println);
        sqlSession.close();
    }

    // 多对一关联
    private static void b1() {
        InputStream inputStream = App.class.getClassLoader().getResourceAsStream("config.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        EmpMapper empMapper = sqlSession.getMapper(EmpMapper.class);
        Emp emp = empMapper.findById1(7980);
        System.out.println(emp);
        sqlSession.close();
    }

    // 多对一拼接
    private static void b2() {
        InputStream inputStream = App.class.getClassLoader().getResourceAsStream("config.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        EmpMapper empMapper = sqlSession.getMapper(EmpMapper.class);
        // 只能获取到员工信息
        Emp emp = empMapper.findById2(7980);
        // 只能查询到部门信息
        DeptMapper deptMapper = sqlSession.getMapper(DeptMapper.class);
        Dept dept = deptMapper.findById(emp.getDeptNo());
        // 组装
        emp.setDept(dept);

        System.out.println(emp);
        sqlSession.close();
    }

    // 一对多关联
    private static void c1() {
        InputStream inputStream = App.class.getClassLoader().getResourceAsStream("config.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        DeptMapper deptMapper = sqlSession.getMapper(DeptMapper.class);
        Dept dept = deptMapper.findById1(10);
        System.out.println(dept);
        sqlSession.close();
    }
    // 一对多拼接
    private static void c2() {
        InputStream inputStream = App.class.getClassLoader().getResourceAsStream("config.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        // 查询部门信息
        DeptMapper deptMapper = sqlSession.getMapper(DeptMapper.class);
        Dept dept = deptMapper.findById2(10);
        // 查询员工信息
        EmpMapper empMapper = sqlSession.getMapper(EmpMapper.class);
        Set<Emp> emps = empMapper.findByDeptNo(10);
        // 组装
        dept.setEmps(emps);
        System.out.println(dept);
        sqlSession.close();
    }

    public static void main(String[] args) {
        c2();
    }
}